import plugin from'../../../lib/plugins/plugin.js'
import{segment}from'oicq'
import fs from 'fs'
import schedule from 'node-schedule'
import YAML from 'yaml'
import Yaml from '../model/Yaml.js'
import cfg from'../../../lib/config/config.js'
import { Config} from '../components/index.js'
import common from'../../../lib/common/common.js'


const path=process.cwd()
// let MasterQQ = 戳一戳主人
// let 戳主人禁言时间 = "60"//单位秒


let _path='./plugins/jixiang/config/只听我的_群号.yaml'
if (!fs.existsSync(_path)) {fs.writeFileSync(_path,'')}

const tp_path=path+'/plugins/jixiang/config/戳一戳/戳一戳图片/';

let pz_path=path+'/plugins/jixiang/config/戳一戳/戳一戳.yaml'

if (!fs.existsSync(tp_path)) {fs.mkdirSync(tp_path)}

let _num =await redis.get('hs:cyc')

if(!_num){
  await redis.set('hs:cyc','0')
  logger.info('〖小吉祥插件〗初始化群聊被戳次数')
}

let cyc_rule =`0 0 0 * * ?`
schedule.scheduleJob(cyc_rule, () => {
    logger.info('〖小吉祥插件〗清空群聊被戳次数排名');
     redis.set('hs:cyc','0')
})




export class hs_chuo extends plugin{
    constructor(){
    super({
        name: 'jixiang群聊戳一戳',
        dsc: '戳一戳',
        event: 'notice.group.poke',
        priority: 1,
        rule: [
            {
                /** 命令正则匹配 */
                fnc: 'chuoyichuo'
                }
            ]
        }
    )
}


async chuoyichuo (e){

if(!Config.getConfig('set','pz')['cyc']){ return false}
//判断只听我的
let group = await Yaml.getread(_path)
   try {
        for (let qqq of group) {
       if(e.group_id == qqq){
       if(e.operator_id ==cfg.masterQQ[0]){
          }else{
          return
          }
          }
        }
      } catch (e) {}

      
          //获取数据
          let data=await Yaml.getread(pz_path)
          let 文字回复列表=data.戳一戳文字回复列表
          let 戳一戳是否撤回=data.戳一戳是否撤回
          let 戳一戳文字回复概率=data.戳一戳文字回复概率
          let 戳一戳其他回复概率=data.戳一戳其他回复概率
          let 戳一戳图片回复概率=data.戳一戳图片回复概率
          let 戳一戳禁言时间=data.戳一戳禁言时间
          let 戳一戳撤回时间=data.戳一戳撤回时间
          let MasterQQ =data.戳一戳主人 
          let 之和=戳一戳文字回复概率+戳一戳其他回复概率+戳一戳图片回复概率
          if(!(之和==100))
          {return logger.info('\x1B[36m戳一戳3个概率之和不等于100\x1B[0m')}
    


      let i = Math.ceil(Math.random() * 100);
      let msg ;
      if (e.operator_id === MasterQQ && e.target_id === MasterQQ ){
        console.log(`主人在做什么坏坏的事情~`);

        msg = [segment.at(e.operator_id ),
        "\n力道刚刚好，懵逼不伤脑~~",
        segment.image(path+`/plugins/jixiang/resources/可莉语音/霍霍.gif`)]
    //引用消息发送
    await e.reply(msg);
    //阻止消息不再向下
    return true;
        
      }else if(e.target_id == MasterQQ && e.target_id != e.operator_id){
        if(i <= 20){
          console.log (`主人有人要对你做坏坏的事情！`);

        msg = [segment.at(e.operator_id ),

        `\n你戳我主人做什么，一定图谋不轨！嗯！一定！！！！`,
        segment.image(path+`/plugins/jixiang/resources/可莉语音/生气.gif`)]

        //引用消息发送
        await e.reply(msg);
        return true;
          
        }else if(i <= 40){
          console.log(`主人有人要对你做坏坏的事情！`);

        msg = [ 

        await this.reply('我说——！'),
        setTimeout (async () => {
        await this.reply('不准戳——！')
        }, 500),
        setTimeout (async () => {
        await this.reply('我要生气了——！')
        }, 500),
        setTimeout (async () => {
        await this.reply([segment.image(path+`/plugins/jixiang/resources/可莉语音/生气.jpg`)])
        }, 500)]

        //引用消息发送
        await e.reply(msg);
        //阻止消息不再向下
        return true;
        }else if(i <= 60 ){
          console.log(`主人有人要对你做坏坏的事情！`);
        msg = [segment.at(e.operator_id ),
        "\n不准戳，我要戳回来！",
        
        segment.image(path+`/plugins/jixiang/resources/可莉语音/阴谋.gif`)],
        //这里应该留一个间隔，延迟戳回去，以后再说吧【希望别处bug】
        await common.sleep(2000),
        setTimeout (async () => { await e.group.pokeMember(e.operator_id)},500)

        //引用消息发送
        await e.reply(msg);
        //阻止消息不再向下
        return true;
        }else if(i <= 80){
          console.log(`主人有人要对你做坏坏的事情！`);

        msg = [segment.at(e.operator_id ),

        "\n留着禁言用",
        segment.image(path+`/plugins/jixiang/resources/可莉语音/霍霍（摇头）.gif`)]
        await e.group.muteMember(e.operator_id, 戳一戳禁言时间)

        //引用消息发送
        await e.reply(msg);
        //阻止消息不再向下
        return true;
        }else if(i <= 100){
          console.log(`主人有人要对你做坏坏的事情！`);

        msg = [segment.at(e.operator_id ),

        "\n好听吗？好听就是好头！",
        segment.image(path+`/plugins/jixiang/resources/可莉语音/霍霍.gif`)]

        //引用消息发送
        await e.reply(msg);
        //阻止消息不再向下
        }
      }

// if (await redis.get(`Yunzai:ShutUp${e.group_id}`)) {
            // return true
        // }
if(e.target_id == cfg.qq){
        await redis.INCR('hs:cyc')//累加
        //生成的随机数
        let i = Math.ceil(Math.random()*100)

        // //获取数据
        // let data=await Yaml.getread(pz_path)
        // let 文字回复列表=data.戳一戳文字回复列表
        // let 戳一戳是否撤回=data.戳一戳是否撤回
        // let 戳一戳文字回复概率=data.戳一戳文字回复概率
        // let 戳一戳其他回复概率=data.戳一戳其他回复概率
        // let 戳一戳图片回复概率=data.戳一戳图片回复概率
        // let 戳一戳禁言时间=data.戳一戳禁言时间
        // let 戳一戳撤回时间=data.戳一戳撤回时间
        // let 之和=戳一戳文字回复概率+戳一戳其他回复概率+戳一戳图片回复概率
        // if(!(之和==100))
        // {return logger.info('\x1B[36m戳一戳3个概率之和不等于100\x1B[0m')}
  
        //回复随机文字

   if(i < 戳一戳文字回复概率){
      
            let text_number = Math.ceil(Math.random() * 文字回复列表['length'])-1
          let az =  await e.reply(文字回复列表[text_number])
          if(戳一戳是否撤回){
            await common.sleep(戳一戳撤回时间);
           e.group.recallMsg(az.message_id);
         }
       }
        
        
         //回复随机图片
   else if(i < (戳一戳文字回复概率 + 戳一戳图片回复概率)){
        let photo_name=await fs.readdirSync(tp_path)
        // logger.info(photo_name)
        if(!photo_name['length']){return logger.info('\x1B[36m戳一戳图片目录没得图片\x1B[0m')}
        let photo_number = Math.ceil(Math.random() * photo_name['length'])-1
        let haha = await e.reply(segment.image(tp_path+photo_name[photo_number]))
        if(戳一戳是否撤回){
          await common.sleep(戳一戳撤回时间);
           e.group.recallMsg(haha.message_id);
           }
        }
   else{
let k= Math.ceil(Math.random()*5)

if(k==5){
let 次数=await redis.get('hs:cyc')
let 名字=Config.getConfig('set','mz')['botname']
let gg= await e.reply(`${名字}今天在群里已经被戳了${次数}次了，再戳就要坏掉啦！`)
            if(戳一戳是否撤回){
             await common.sleep(戳一戳撤回时间);
           e.group.recallMsg(gg.message_id);
         }
}
//禁言
if (k==4){
         let haha3 =   await  e.reply('不！！')
           await common.sleep(500);
         let haha4 =    await  e.reply('准！！')
           await common.sleep(500);
         let haha5 =    await   e.reply('戳！！')
           await common.sleep(1000);
        await e.group.muteMember(e.operator_id, 戳一戳禁言时间)
         let haha6 = await e.reply(segment.image(path+'/plugins/jixiang/resources/可莉语音/'+'sq.jpg'))
   if(戳一戳是否撤回){
  await common.sleep(戳一戳撤回时间);
  e.group.recallMsg(haha3.message_id);
  e.group.recallMsg(haha4.message_id);
  e.group.recallMsg(haha5.message_id);
  e.group.recallMsg(haha6.message_id);
        }
   }
//禁言2
  else if(k==3){    
  let heihei = await  e.reply('哼哼唧唧！有完没完了！(◦`~´◦)')
  await e.group.muteMember(e.operator_id, 戳一戳禁言时间)
  if(戳一戳是否撤回){
  await common.sleep(戳一戳撤回时间);
  e.group.recallMsg(heihei.message_id);
   }
  }
//爬，表情
  else if(k==0||k==1){
    let har = await e.reply(await segment.image(`https://www.dreamling.xyz/API/qq_pa/api.php?qq=${e.operator_id}`))
    await e.group.muteMember(e.operator_id, 戳一戳禁言时间)
     if(戳一戳是否撤回){
            await common.sleep(戳一戳撤回时间);
            e.group.recallMsg(har.message_id);
           }
        }

//反击
  else if(k==2){
          let haha = await  e.reply('哼💢你戳我，我也要戳你！')
            await common.sleep(600)
            await e.group.pokeMember(e.operator_id)
            if(戳一戳是否撤回){
             await common.sleep(戳一戳撤回时间);
           e.group.recallMsg(haha.message_id);
         }
        }
        
     }
    
}
   return true
}
}
